﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OldPlatform_to_NewPlatform
{
    public class DateTimeUtils
    {
        /// <summary>
        /// 日期时间转换为毫秒时间戳
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns></returns>
        public static long DateTimeToLongTimeStamp(DateTime dateTime)
        {
            return (long)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0))).TotalMilliseconds;
        }

        /// <summary>
        /// 日期时间转换为秒时间戳
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns></returns>
        public static long DateTimeToTimeStamp(DateTime dateTime)
        {
            return (long)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        }

        /// <summary>
        /// 毫秒时间戳转换为日期时间
        /// </summary>
        public static DateTime LongTimeStampToDateTime(long timeStamp)
        {
            return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0)).AddMilliseconds(timeStamp);
        }

        /// <summary>
        /// 秒时间戳转换为日期时间
        /// </summary>
        public static DateTime TimeStampToDateTime(long timeStamp)
        {
            return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(timeStamp);
        }

        /// <summary>
        /// 旧日期时间带毫秒转换为新日期时间
        /// </summary>
        public static DateTime OldTimeToNewtime(DateTime dt)
        {
            // 将旧日期时间转换为秒时间戳
            long timeStamp = DateTimeToTimeStamp(dt);
            // 将秒时间戳进行运算，再(+4 / 10)*10+5
            long newTimeStamp = ((timeStamp + 4) / 10) * 10 + 5;
            // 将时间戳
            // 将新时间戳转换为日期时间
            return TimeStampToDateTime(newTimeStamp);
        }

        /// <summary>
        /// 旧日期时间带毫秒转换为新日期时间
        /// </summary>
        public static DateTime OldMillisecondsTimeToNewtime(DateTime dt)
        {
            // 将旧日期时间转换为毫秒时间戳
            long timeStamp = DateTimeToLongTimeStamp(dt);
            // 将豪秒时间戳进行运算，再(+4 / 10)*10+5
            long newTimeStamp = (((timeStamp / 1000 + 4) / 10) * 10 + 5) * 1000;
            // 将新时间戳转换为日期时间
            return TimeStampToDateTime(newTimeStamp);
        }

        /// <summary>
        /// 将秒数转换为时分秒
        /// </summary>
        public static string SecondToTime(long second)
        {
            long hour = second / 3600;
            long minute = (second % 3600) / 60;
            long sec = second % 60;
            string time = "";
            if (hour > 0) { time += hour + "时"; }
            if (minute > 0) { time += minute + "分"; }
            if (sec > 0) { time += sec + "秒"; }
            return time;
        }

        /// <summary>
        /// 将秒数转换为时分秒
        /// </summary>
        public static string SecondToTime(double second)
        {
            long sec = (long)second;
            return SecondToTime(sec);
        }
    }
}